/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef NSObject_H_
#define NSObject_H_

#include "wx/wxprec.h"

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include <map>
#include <set>

#define NSUNUSED(a) {(void)(a);}

class NSKeyedUnarchiver;
class NSObject;
class NS_Binding;
class __NS_KVO_Proxy;
typedef std::map<NSObject*,__NS_KVO_Proxy*> observers_map;
typedef std::set<wxString> manual_observers_set;
typedef std::map<wxString,observers_map*> kvo_map;
typedef std::map<wxString,NS_Binding*> bindings_map;

class NSObject : public wxObject
{
	DECLARE_DYNAMIC_CLASS(NSObject)
public:
	NSObject();
	virtual void initWithUnarchiver(NSKeyedUnarchiver *archiver) {archiver = archiver;}
	virtual NSObject* nibInstantiate(NSKeyedUnarchiver* archiver) {NSUNUSED(archiver);return this;}
	virtual void awakeFromNib(void) {}

	static NSObject *createDynamicObject(wxString &className)
	{
		wxASSERT(className.StartsWith(wxT("NS")) || className.StartsWith(wxT("_NS")));
		NSObject *obj = wxDynamicCast(wxCreateDynamicObject(className), NSObject);
		wxASSERT(obj);
		return obj;
	}

	/* KVC */
	void setValue(const wxString& key, wxObject *value);
	void setValueForKeypath(const wxString& keypath, wxObject *value);
	void setValue(const wxString& key, bool value)
	{
		setValue(key, new wxVariant(value));
	}
	wxObject* getValue(const wxString& key);
	wxObject* getValueForKeypath(const wxString& keypath);
	bool getBoolValue(const wxString& key)
	{
		wxVariant *v = wxDynamicCast(getValue(key), wxVariant);
		wxASSERT(v);
		return v->GetBool();
	}

	/* KVO */
	virtual void registerObserver(NSObject *observer, const wxString& keypath, int options = 0, void *context = NULL);
	virtual void removeObserver(NSObject *observer, const wxString& keypath);
	virtual void observeValueForKeypath(const wxString &keypath, NSObject* object, void *context);

	/* bindings */
	virtual void bind(const wxString& bind, NSObject* target, const wxString& keypath, NSObject* options = NULL);
	virtual void unbind(const wxString& bind);

	/* actions */
	virtual void sendAction(NSObject* target, const wxString& action, wxObject* arg);
	virtual void bindingInvokeAction(const wxString& targetKey, const wxString& argumentKey);

	static wxPoint bounds2wxPoint(wxWindow *parent, const wxRect& bounds);

protected:
	/* KVC */
	virtual void _setValue(const wxString& key, wxObject *value);
	virtual wxObject* _getValue(const wxString& key);

	/* KVO */
	kvo_map* _observers;
	virtual void _willValueChangedForKey(const wxString &key);
	virtual void _didValueChangedForKey(const wxString &key);
	manual_observers_set* _manual_observers;
	void disableAutomaticObservation(const wxString &key);


	/* bindings */
	bindings_map *_bindings;
	void exposeBinding(const wxString& bind);

	/* actions */
	virtual void _callAction(const wxString& action, wxObject* arg, NSObject* caller);
};

#define NSMultipleValuesMarker ((NSObject*)13)
#define NSNoSelectionMarker ((NSObject*)15)
#define NSNotApplicableMarker ((NSObject*)17)

#endif